/*******************************************************************************
* This file provides the API for the following sensors:
* ~ ADXL345
* ~ ITG-3200
* ~ HMC5843
*
* Author: Kong Wai Weng
* Email:  waiweng83@gmail.com
*******************************************************************************/



#ifndef _HAL_SENSORS_H
#define _HAL_SENSORS_H



#include "system.h"
#include "hal_i2c.h"
#include "variables.h"



/*******************************************************************************
* STRUCTURE AND UNION DEFINITION                                               *
*******************************************************************************/

// Accelerometer data (raw).
struct ACC_DATA_RAW {
	signed int siAccX;
	signed int siAccY;
	signed int siAccZ;
};

// Accelerometer data (G).
struct ACC_DATA {
	float fAccX;
	float fAccY;
	float fAccZ;
};



// Gyro data (raw).
struct GYRO_DATA_RAW {
	signed int siGyroX;
	signed int siGyroY;
	signed int siGyroZ;
};

// Gyro data (rad/s).
struct GYRO_DATA {
	float fGyroX;
	float fGyroY;
	float fGyroZ;
};



// Magnetometer data (raw).
struct MAG_DATA_RAW {
	signed int siMagX;
	signed int siMagY;
	signed int siMagZ;
};

// Magnetometer data (Range: -1 to +1).
struct MAG_DATA {
	float fMagX;
	float fMagY;
	float fMagZ;
};



/*******************************************************************************
* PUBLIC FUNCTION PROTOTYPES                                                   *
*******************************************************************************/

/*******************************************************************************
* PUBLIC FUNCTION: vInitSensors
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Initialize the sensors.
* This function must be called after the I2C has been initialized.
*
*******************************************************************************/
void vInitSensors(void);



/*******************************************************************************
* PUBLIC FUNCTION: xReadAccRaw
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ The raw data from the accelerometer.
*
* DESCRIPTIONS:
* Read the raw data from the accelerometer.
*
*******************************************************************************/
struct ACC_DATA_RAW xReadAccRaw(void);



/*******************************************************************************
* PUBLIC FUNCTION: xReadAcc
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ The scaled and calibrated accelerometer data.
* ~ Unit: G (9.8m/s).
*
* DESCRIPTIONS:
* Read the scaled and calibrated accelerometer data.
* System config must be loaded into RAM before calling this function.
*
*******************************************************************************/
struct ACC_DATA xReadAcc(void);



/*******************************************************************************
* PUBLIC FUNCTION: xReadGyroRaw
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ The raw data from the gyro.
*
* DESCRIPTIONS:
* Read the raw data from the gyro.
*
*******************************************************************************/
struct GYRO_DATA_RAW xReadGyroRaw(void);



/*******************************************************************************
* PUBLIC FUNCTION: xReadGyro
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ The scaled and calibrated gyro data.
* ~ Unit: rad/s.
*
* DESCRIPTIONS:
* Read the scaled and calibrated gyro data.
* System config must be loaded into RAM before calling this function.
*
*******************************************************************************/
struct GYRO_DATA xReadGyro(void);



/*******************************************************************************
* PUBLIC FUNCTION: xReadMagRaw
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ The raw data from the magnetometer.
*
* DESCRIPTIONS:
* Read the raw data from the magnetometer.
*
*******************************************************************************/
struct MAG_DATA_RAW xReadMagRaw(void);



/*******************************************************************************
* PUBLIC FUNCTION: xReadMag
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ The scaled and calibrated magnetometer data.
* ~ Value is scaled to within -1 to +1.
*
* DESCRIPTIONS:
* Read the scaled and calibrated magnetometer data.
* System config must be loaded into RAM before calling this function.
*
*******************************************************************************/
struct MAG_DATA xReadMag(void);



/*******************************************************************************
* PUBLIC FUNCTION: vCalibrateMag
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Calibrate the magnetometer.
*
*******************************************************************************/
void vCalibrateMag(void);



#endif
